package com.ruoyi.shop.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.shop.mapper.ShopUserCartMapper;
import com.ruoyi.shop.domain.ShopUserCart;
import com.ruoyi.shop.service.IShopUserCartService;
import com.ruoyi.common.core.text.Convert;

/**
 * 购物车Service业务层处理
 * 
 * @author Alan Zhang
 * @date 2021-09-14
 */
@Service
public class ShopUserCartServiceImpl implements IShopUserCartService 
{
    @Autowired
    private ShopUserCartMapper shopUserCartMapper;

    /**
     * 查询购物车
     * 
     * @param id 购物车主键
     * @return 购物车
     */
    @Override
    public ShopUserCart selectShopUserCartById(Long id)
    {
        return shopUserCartMapper.selectShopUserCartById(id);
    }

    /**
     * 查询购物车列表
     * 
     * @param shopUserCart 购物车
     * @return 购物车
     */
    @Override
    public List<ShopUserCart> selectShopUserCartList(ShopUserCart shopUserCart)
    {
        return shopUserCartMapper.selectShopUserCartList(shopUserCart);
    }

    /**
     * 新增购物车
     * 
     * @param shopUserCart 购物车
     * @return 结果
     */
    @Override
    public int insertShopUserCart(ShopUserCart shopUserCart)
    {
        shopUserCart.setCreateTime(DateUtils.getNowDate());
        return shopUserCartMapper.insertShopUserCart(shopUserCart);
    }

    /**
     * 批量新增购物车
     *
     * @param shopUserCartList 购物车商品
     * @return 结果
     */
    @Override
    public int batchShopUserCart(List<ShopUserCart> shopUserCartList){
        return shopUserCartMapper.batchShopUserCart(shopUserCartList);
    }

    /**
     * 修改购物车
     * 
     * @param shopUserCart 购物车
     * @return 结果
     */
    @Override
    public int updateShopUserCart(ShopUserCart shopUserCart)
    {
        shopUserCart.setUpdateTime(DateUtils.getNowDate());
        return shopUserCartMapper.updateShopUserCart(shopUserCart);
    }

    /**
     * 批量删除购物车
     * 
     * @param ids 需要删除的购物车主键
     * @return 结果
     */
    @Override
    public int deleteShopUserCartByIds(String ids)
    {
        return shopUserCartMapper.deleteShopUserCartByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除购物车信息
     * 
     * @param id 购物车主键
     * @return 结果
     */
    @Override
    public int deleteShopUserCartById(Long id)
    {
        return shopUserCartMapper.deleteShopUserCartById(id);
    }

    /**
     * 批量删除购物车
     *
     * @param userId 用户Id
     * @param ids 需要删除的数据 商品ID集合
     * @return 结果
     */
    @Override
    public int deleteShopUserCartByProductIds(Long userId, String[] ids){
        HashMap<String, Object> params = new HashMap<>();
        params.put("ids",ids);
        params.put("userId",userId);
        return shopUserCartMapper.deleteShopUserCartByProductIds(params);
    }

    /**
     * 选中 或反选
     * @param productIds
     * @param isChecked
     * @param userId
     */
    @Override
    public int updateCheck(String[] productIds, Integer isChecked, Long userId) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("productIds", productIds);
        params.put("userId", userId);
        params.put("isChecked", isChecked);
        return shopUserCartMapper.updateCheck(params);
    }

    /**
     * 统计用户 购物车数量
     * @param userId
     * @return
     */
    @Override
    public Integer selectShopUserCartCountByUserId(Long userId){
        return shopUserCartMapper.selectShopUserCartCountByUserId(userId);
    }
}
